package com.hyfrogx.modules.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.app.form.video.SmallVideoCommentAddForm;
import com.hyfrogx.modules.app.service.UserService;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallCommentEntiy;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallLikeEntity;
import com.hyfrogx.modules.manage.service.HyVideoSmallCommentService;
import com.hyfrogx.modules.manage.service.HyVideoSmallLikeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName ApiSmallVideoController
 * @Description TODO
 * @Author yangjun
 * @date 2019/12/6 17:24
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/smallVideo")
@Api(value = "ApiSmallVideoController", tags = {"客户端--小视频"})
public class ApiSmallVideoController {

    @Autowired
    HyVideoSmallCommentService hyVideoSmallCommentService;

    @Autowired
    HyVideoSmallLikeService hyVideoSmallLikeService;


    @Autowired
    UserService userService;

    //小视频评论
    @Login
    @PostMapping(value = "comment")
    public R comment(@RequestAttribute(value = "userId") Long userId,SmallVideoCommentAddForm form) {
        try {
            ValidatorUtils.validateEntity(form);

            User user = userService.getById(userId);
            HyVideoSmallCommentEntiy target=new HyVideoSmallCommentEntiy();
            BeanUtil.copyProperties(form,target);
            target.setCreateTime(new Date());
            target.setUid(userId);
            target.setUsername(user.getUsername());
            target.setPhone(user.getPhone());
            hyVideoSmallCommentService.getBaseMapper().insert(target);
            return R.ok("评论成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    //小视频点赞
    @Login
    @PostMapping(value = "like/{id}")
    public R like(@RequestAttribute(value = "userId") Long userId, @PathVariable Integer id) {
        try {
            HyVideoSmallLikeEntity result = hyVideoSmallLikeService.getOne(new QueryWrapper<HyVideoSmallLikeEntity>().eq("uid",userId).eq("wid",id));
            if(result!=null){
                return R.error("请勿重复操作");
            }
            HyVideoSmallLikeEntity entity=new HyVideoSmallLikeEntity();
            entity.setUid(userId);
            entity.setWid(id);
            hyVideoSmallLikeService.getBaseMapper().insert(entity);
            return R.ok("点赞成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
